package com.example.administrator.dc.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.dc.Activity.PhotoDetail;
import com.example.administrator.dc.Bean.CancelFollow;
import com.example.administrator.dc.Bean.Follow;
import com.example.administrator.dc.Bean.IsFollow;
import com.example.administrator.dc.Bean.UserPhoto;
import com.example.administrator.dc.R;
import com.example.administrator.dc.fragment.UserphotoFragment;
import com.example.administrator.dc.helper.RoundImageView;
import com.example.administrator.dc.model.FollowModel;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/8/22.
 */

public class UserPhotoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    private  PopupWindow mPop;
    private int result;
    private String userid;
    private String followid;





    public UserPhotoListAdapter(Context mContext , List mDataList){
       this. mContext=mContext;
        this.mDataList=mDataList;
        //this.mLayoutInflater=LayoutInflater.from(mContext);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_userphoto, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final UserPhoto entity = (UserPhoto) mDataList.get(position);
        if (null == entity)
            return;
        final UserPhotoListAdapter.ViewHolder viewHolder = (UserPhotoListAdapter.ViewHolder) holder;
        viewHolder.user_name.setText(entity.getAliasname());
        viewHolder.user_date.setText(entity.getCreatetime());
        viewHolder.user_content.setText(entity.getTitle());
        if (!"".equals(entity.getPhoto()))
            Picasso.with(mContext).load(entity.getPhoto()).into(viewHolder.user_image);
        if (!"".equals(entity.getPic()))
            Picasso.with(mContext).load(entity.getPic()).into(viewHolder.user_userphoto);
        if(!"0".equals(entity.getGender()+"")){
            Picasso.with(mContext).load(R.drawable.female).into(viewHolder.user_gender);}
        else
        { Picasso.with(mContext).load(R.drawable.male).into(viewHolder.user_gender);}



        viewHolder.user_action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //                Toast.makeText(mContext, position + "被点击了", Toast.LENGTH_SHORT).show();
                int zanWidth = viewHolder.user_action.getWidth();
                int zanHeight = viewHolder.user_action.getHeight();
                View contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_layout, null);
                TextView popZan = (TextView) contentView.findViewById(R.id.like);
                TextView popPinlun = (TextView) contentView.findViewById(R.id.comment);
                popZan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "点赞", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(mContext, UserphotoFragment.class);
                        mContext .startActivity(intent);

                    }
                });
                popPinlun.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "评论", Toast.LENGTH_SHORT).show();
                    }
                });

                if (mPop == null) { //优化:当popwindow为空的时候才创建,不为空,直接复用
                    int width = zanWidth * 8; //设置PopupWindow的宽
                    int height = zanHeight; //设置PopupWindow的高
                    boolean focusable = true; //设置PopupWindow可获取焦点
                    mPop = new PopupWindow(contentView, width, height, focusable);
                    //为PopupWindow设置背景,获取焦点才能生效(谷歌官方要求)
                    mPop.setBackgroundDrawable(new ColorDrawable());

                }
                View anchor = viewHolder.pop_show; //指定PopupWindow在哪个控件下面显示
                int xoff = zanWidth ;//指定PopupWindow在x轴方向上的偏移量
                int yoff = -zanHeight;//指定PopupWindow在Y轴方向上的偏移量
                mPop.showAsDropDown(anchor, xoff, yoff);



        }
        });
        getUser();
        if (!"".equals(userid))
        {
            //判断是否关注
            followid=entity.getUid()+"";
            final FollowModel followModel=new FollowModel();
            Call call=followModel.isFollow(userid,followid);
            call.enqueue(new Callback<IsFollow>() {
                @Override
                public void onResponse(final Call <IsFollow> call, Response<IsFollow> response) {
                    IsFollow res=response.body();
                    result =res.getStatus();
                    if (result==0)
                    {
                        viewHolder.btn_follow.setText("关注");

                    }
                    else
                    {
                        viewHolder.btn_follow.setText("取消关注");
                    }

                }

                @Override
                public void onFailure(Call<IsFollow> call, Throwable t) {
                    Toast.makeText(mContext,"网络错误1",Toast.LENGTH_SHORT).show();
                }
            });

            //点击关注取消关注
            viewHolder.btn_follow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //判断是否关注
                    followid=entity.getUid()+"";
                    final FollowModel followModel=new FollowModel();
                    Call call=followModel.isFollow(userid,followid);
                    call.enqueue(new Callback<IsFollow>() {
                        @Override
                        public void onResponse(final Call <IsFollow> call, Response<IsFollow> response) {
                            final IsFollow res=response.body();
                            result =res.getStatus();
                            if (result==0)
                            {
                                viewHolder.btn_follow.setText("关注");
                                Call call1=followModel.follow(userid,followid);
                                call1.enqueue(new Callback<Follow>() {
                                    @Override
                                    public void onResponse(Call<Follow> call, Response<Follow> response) {
                                        Follow tmp =response.body();
                                        int stutas=tmp.getStatus();
                                        if (stutas==1)
                                        {
                                            viewHolder.btn_follow.setText("取消关注");
                                            Toast.makeText(mContext,"关注成功",Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<Follow> call, Throwable t) {

                                    }
                                });

                            }
                            else
                            {
                                viewHolder.btn_follow.setText("取消关注");
                                Call call1=followModel.cancelFollow(userid,followid);
                                call1.enqueue(new Callback<CancelFollow>() {
                                    @Override
                                    public void onResponse(Call<CancelFollow> call, Response<CancelFollow> response) {
                                        CancelFollow tmp =response.body();
                                        int stutas=tmp.getStatus();
                                        if (stutas==1)
                                        {
                                            viewHolder.btn_follow.setText("关注");
                                            Toast.makeText(mContext,"取消成功",Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<CancelFollow> call, Throwable t) {

                                    }
                                });

                            }

                        }

                        @Override
                        public void onFailure(Call<IsFollow> call, Throwable t) {
                            Toast.makeText(mContext,"网络错误2",Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            });

        }
        else {


 viewHolder.btn_follow.setText("关注");
            viewHolder.btn_follow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(mContext,"请先登录在进行关注！",Toast.LENGTH_SHORT).show();
                }
            });


        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(mContext,PhotoDetail.class);
                intent.putExtra("photo",entity.getPhoto());
                mContext.startActivity(intent);

            }
        });


    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView user_name;
        RoundImageView user_userphoto;
        TextView user_date;
        ImageView user_gender;
       ImageView user_image;

        TextView user_content;
        ImageView user_action;
        TextView pop_show;
        Button btn_follow;

        public ViewHolder(View itemView) {
            super(itemView);
            user_name = (TextView) itemView.findViewById(R.id.userphoto_cardview_username);
            user_userphoto = (RoundImageView) itemView.findViewById(R.id.userphoto_cardview_userphoto_img);
            user_date = (TextView) itemView.findViewById(R.id.userphoto_cardview_datetime);
            user_gender= (ImageView) itemView.findViewById(R.id.userphoto_cardview_gender_img);
            user_image=(ImageView) itemView.findViewById(R.id.userphoto_cardview_photo);
            user_content=(TextView) itemView.findViewById(R.id.userphoto_cardview_content);
            user_action= (ImageView) itemView.findViewById(R.id.userphoto_cardview_action_btn);
            pop_show = (TextView) itemView.findViewById(R.id.pop_show);
            btn_follow=(Button)itemView.findViewById(R.id.btn_userphoto_follow);

        }
    }

    private void getUser(){
        SharedPreferences sp = mContext.getSharedPreferences("UserInfo", mContext.MODE_PRIVATE);
        userid = sp.getString("user_id", "");


    }




}
